package main

import "fmt"

func main() {
	fmt.Println("Creating slice......")

	//arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
	//s1 := arr[2:6] // 2,3,4,5
	////s2 := s1[3:5]  // 5,6
	////s1[4]  报错
	//fmt.Println(s1, arr)
	//s1 = append(s1, 123, 456, 789)
	//s1 = append(s1, 123, 456, 789)
	//fmt.Println(s1, arr)
	//slice 可以向后扩展
	//s[i] 不可以超过 Len(s)
	//	向后扩展不能超过底层数组 cap(s)
	//fmt.Println(arr)
	//fmt.Printf("s1=%v , len(s1) = %d , cap(s1) = %d \n\n", s1, len(s1), cap(s1))
	//fmt.Printf("s2=%v , len(s2) = %d , cap(s2) = %d \n\n", s2, len(s2), cap(s2))

	//s3 := append(s2, 10) // 预期 5,6,10
	//s4 := append(s3, 11) // 预期 5,6,10,11
	//s5 := append(s4, 12)// 预期 5,6,10,11,12

	//fmt.Println("s3 , s4 , s5 = ", s3, s4, s5) // s3 , s4 , s5 =  [5 6 10] [5 6 10 11] [5 6 10 11 12]
	// s4 s5 对应的 不是 arr ，分配了一个新的 arr 更加长的 arr
	// 添加元素超过 cap ，原来的数组没有使用会被 垃圾回收掉，系统会重新分配更大的底层数组
	//fmt.Println("arr = ", arr) // arr =  [0 1 2 3 4 5 6 10]

	//var s []int // Zero Value = nil
	//
	//for i := 0; i < 100; i++ {
	//	printSlice(s)
	//	s = append(s, 2*i+1)
	//}
	//fmt.Println(s)

	//s1 := []int{2, 4, 6, 8}
	////printSlice(s1)
	//s2 := make([]int, 16)
	//s3 := make([]int, 10, 32) // len=10 , cap=32
	//fmt.Println(s2,s3)
	//printSlice(s2)
	//printSlice(s3)

	//fmt.Println("Copying slice......")
	//copy(s2, s1)
	//fmt.Println("copy::",s2,s1)
	//printSlice(s2)
	//fmt.Println("Deleting elements from slice......")
	////s2[:3]  + s2[4:]
	//s2 = append(s2[:3], s3[4:]...)
	//printSlice(s2)
	//fmt.Println("s2::",s2)
	//fmt.Println("s1::",s1)
	//
	//fmt.Println("Popping from front") // 删除头
	//s2 = s2[1:]
	//printSlice(s2)
	//fmt.Println("Popping from back") // 删除尾
	//s2 = s2[:len(s2)-1]
	//printSlice(s2)

	//arr := [5]int{0, 1, 2, 3, 4}
	//slice1 := arr[:]
	//slice1 = append(slice1[:2], slice1[3:]...)
	//fmt.Println("slice1::", slice1)
	//fmt.Println("arr::", arr)

	//s2 := []int{2,4,6,8,0,0,0,0,0,0,0,0,0,0,0,0} // len=16 , cap=16
	//front := s2[0] // 获取开头第一个元素
	//s2 = s2[1:] // len=15 , cap=16
	//printSlice(s2)
	//fmt.Println(front)
	//fmt.Println(s2)
	////s2 := []int{2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // len=16 , cap=16
	//tail := s2[len(s2)-1]                                       // 获取结尾第一个元素
	//s2 = s2[:len(s2)-1]
	//printSlice(s2)
	//fmt.Println(tail)
	//fmt.Println(s2)

	//arr := [...]int{0, 1, 2, 3, 4, 5}
	//s1 := arr[1:2]
	//s2 := s1[3:4]
	//fmt.Printf("arr--cap= %d , arr-len= %d\n", cap(arr), cap(arr))
	//s3 := append(s2, 10) // 预期 5,6,10
	//fmt.Printf("arr--cap= %d , arr-len= %d\n", cap(arr), cap(arr))
	////s4 := append(s3, 11) // 预期 5,6,10,11
	////s5 := append(s4, 12)// 预期 5,6,10,11,12
	//
	//fmt.Println(s3)
	//
	////fmt.Println("s3 , s4 , s5 = ", s3, s4, s5) // s3 , s4 , s5 =  [5 6 10] [5 6 10 11] [5 6 10 11 12]
	//fmt.Println("arr = ", arr) // arr =  [0 1 2 3 4 5 6 10]
	//fmt.Println(arr[:len(arr) ])

	arr := [8]int{0, 1, 2, 3, 4, 5, 6, 7}
	s2 := arr[2:5]
	s2 = append(s2, 10, 10, 10, 10)
	s3 := s2[1:2]
	s3 = append(s3, 10, 10, 10, 10)
	fmt.Println(s2, len(s2), cap(s2))
	fmt.Println(s3, len(s3), cap(s3))
	fmt.Println(arr, len(arr), cap(arr))
}

////func printSlice(s []int) {
////	fmt.Printf("len=%d , cap=%d \n", len(s), cap(s))
////	//len=0 , cap=0
////	//len=1 , cap=1
////	//len=2 , cap=2
////	//len=3 , cap=4
////	//len=4 , cap=4
////	//len=5 , cap=8
////	//len=6 , cap=8
////	//len=7 , cap=8
////	//len=8 , cap=8
////	//len=9 , cap=16
////	//len=10 , cap=16
////	//len=11 , cap=16
////	//len=12 , cap=16
////	//len=13 , cap=16
////	//len=14 , cap=16
////	//len=15 , cap=16
////	//len=16 , cap=16
////	//len=17 , cap=32
////	//len=18 , cap=32
////	//len=19 , cap=32
////	//len=20 , cap=32
////	//len=21 , cap=32
////	//len=22 , cap=32
////	//len=23 , cap=32
////	//len=24 , cap=32
////	//len=25 , cap=32
////	//len=26 , cap=32
////	//len=27 , cap=32
////	//len=28 , cap=32
////	//len=29 , cap=32
////	//len=30 , cap=32
////	//len=31 , cap=32
////	//len=32 , cap=32
////	//len=33 , cap=64
////	//len=34 , cap=64
////	//len=35 , cap=64
////	//len=36 , cap=64
////	//len=37 , cap=64
////	//len=38 , cap=64
////	//len=39 , cap=64
////	//len=40 , cap=64
////	//len=41 , cap=64
////	//len=42 , cap=64
////	//len=43 , cap=64
////	//len=44 , cap=64
////	//len=45 , cap=64
////	//len=46 , cap=64
////	//len=47 , cap=64
////	//len=48 , cap=64
////	//len=49 , cap=64
////	//len=50 , cap=64
////	//len=51 , cap=64
////	//len=52 , cap=64
////	//len=53 , cap=64
////	//len=54 , cap=64
////	//len=55 , cap=64
////	//len=56 , cap=64
////	//len=57 , cap=64
////	//len=58 , cap=64
////	//len=59 , cap=64
////	//len=60 , cap=64
////	//len=61 , cap=64
////	//len=62 , cap=64
////	//len=63 , cap=64
////	//len=64 , cap=64
////	//len=65 , cap=128
////	//len=66 , cap=128
////	//len=67 , cap=128
////	//len=68 , cap=128
////	//len=69 , cap=128
////	//len=70 , cap=128
////	//len=71 , cap=128
////	//len=72 , cap=128
////	//len=73 , cap=128
////	//len=74 , cap=128
////	//len=75 , cap=128
////	//len=76 , cap=128
////	//len=77 , cap=128
////	//len=78 , cap=128
////	//len=79 , cap=128
////	//len=80 , cap=128
////	//len=81 , cap=128
////	//len=82 , cap=128
////	//len=83 , cap=128
////	//len=84 , cap=128
////	//len=85 , cap=128
////	//len=86 , cap=128
////	//len=87 , cap=128
////	//len=88 , cap=128
////	//len=89 , cap=128
////	//len=90 , cap=128
////	//len=91 , cap=128
////	//len=92 , cap=128
////	//len=93 , cap=128
////	//len=94 , cap=128
////	//len=95 , cap=128
////	//len=96 , cap=128
////	//len=97 , cap=128
////	//len=98 , cap=128
////	//len=99 , cap=128
////
////}
